//
//  XSShopCollectionViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSShopCollectionViewController.h"
#import "XSSearchViewController.h"

@interface XSShopCollectionViewController ()
{
    NSArray * titleArr;
    NSInteger  lastIndex;
    UIView * view;
    NSArray * normalArr;
    NSArray * slectArr;
    UIView * _btnView;
    
}
@end

@implementation XSShopCollectionViewController

static NSString * const reuseIdentifier = @"Cell";
-(void)viewWillAppear:(BOOL)animated
{

    
    NSString * urlString =RequestUrl(_url, 0);
    [XSHTTPManager getZeroPlistWithComplection:^(NSMutableArray *array2) {
        _dataArray =array2;
        [self.collectionView reloadData];
        NSLog(@"%@---+++",_url);
    } withUrl:urlString];
    
    self.navigationController.navigationBarHidden =YES;
//    [self.collectionView addPullToRefreshWithActionHandler:^{
//        
//    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
  
    titleArr =@[@"新品",@"热销",@"价格"];
    normalArr =@[@"yuike_itemgroup_arrow_gray",@"yuike_itemgroup_arrow_gray",@"yuike_itemgroup_arrow2_gray"];
    
    slectArr =@[@"yuike_itemgroup_arrow_black",@"yuike_itemgroup_arrow_black",@"yuike_itemgroup_arrow2_blackup",@"yuike_itemgroup_arrow2_blackdown"];
    
    
    [self createImageView];
    [self createButtonView];

    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    }

-(void)createButtonView
{
    _btnView =[[UIView alloc]init];
    _btnView.frame =CGRectMake(0, 200, self.view.frame.size.width, 50);
    [_topView addSubview:_btnView];
    
    UIButton * lastBtn;
    for (int i = 0; i < 3; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag =i+1;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnView addSubview:button];
        
        UILabel * lab =[[UILabel alloc]init];
        lab.font =[UIFont systemFontOfSize:13];
        lab.text =titleArr[i];
        lab.tag =i+10;
        [button addSubview:lab];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.bottom.offset(0);
            
            if (lastBtn) {
                //存在的话说明第二个按钮起...
                make.left.equalTo(lastBtn.mas_right).with.offset(0);
                make.width.equalTo(lastBtn.mas_width);
            }
            else
            {
                //不存在说明第一个
                make.left.offset(0);
            }
            if (i==2) {
                make.right.offset(0);
            }
            
        }];
        
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(10);
            make.bottom.offset(-10);
            make.centerX.equalTo(button.mas_centerX).with.offset(20);
            make.width.equalTo(@60);
        }];
        lastBtn =button;
        
        UIImageView * imageView =[[UIImageView alloc]init];

        imageView.tag =i+20;

        [button addSubview:imageView];
        
        if (i==0)
        {
            lab.textColor =btnColor;
            imageView.image =[UIImage imageNamed:slectArr[i]];
        }
        else
        {
            lab.textColor =[UIColor blackColor];
            imageView.image =[UIImage imageNamed:normalArr[i]];
        }
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(5);
            make.bottom.offset(-5);
            make.centerX.equalTo(lab.mas_centerX).with.offset(15);
            make.width.equalTo(@20);
        }];
        
        
        imageView.userInteractionEnabled =NO;
        lab.userInteractionEnabled = NO;
        
    }
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
//    button2.backgroundColor = [UIColor blueColor];
    
    [button2 addTarget:self action:@selector(Btn5:) forControlEvents:UIControlEventTouchUpInside];
    [_btnView addSubview:button2];
    
    button2.tag =6;
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.bottom.offset(-5);
        make.centerX.equalTo(lastBtn.mas_centerX).with.offset(35);
        make.width.equalTo(@20);
    }];
   _block(_topView);
    
}
-(void)Btn5:(UIButton *)btn
{
    NSLog(@"---%d",lastIndex);

    
    NSString * urlString =RequestUrl(_url, 6);
    [XSHTTPManager getZeroPlistWithComplection:^(NSMutableArray *array2) {
        _dataArray =array2;
        [self.collectionView reloadData];
        
    } withUrl:urlString];
    
    
    UIImageView * image =(UIImageView *)[_btnView viewWithTag:lastIndex+20];
    image.image =[UIImage imageNamed:normalArr[lastIndex]];
    UILabel * lab2 =(UILabel *)[_btnView viewWithTag:lastIndex+10];
    lab2.textColor =[UIColor blackColor];
    
    
    UIImageView * img =(UIImageView *)[_btnView viewWithTag:22];
    img.image =[UIImage imageNamed:slectArr[3]];
    UILabel * lab3 =(UILabel *)[_btnView viewWithTag:12];
    lab3.textColor =btnColor;
    lastIndex =2;
}
-(void)btnClick:(UIButton *)btn
{

    NSInteger  index =btn.tag -1;
    if (index ==2)
    {
        NSString * urlString =RequestUrl(_url, 5);
        [XSHTTPManager getZeroPlistWithComplection:^(NSMutableArray *array2) {
            _dataArray =array2;
            [self.collectionView reloadData];
            
        } withUrl:urlString];
    }
    
    NSString * urlString =RequestUrl(_url, index);
    [XSHTTPManager getZeroPlistWithComplection:^(NSMutableArray *array2) {
        _dataArray =array2;
        [self.collectionView reloadData];
        
    } withUrl:urlString];

    [self changerImage:index];
    
}
-(void)changerImage:(NSInteger)index
{
    if (index ==lastIndex) {
        return;
    }
    if (index ==2)
    {
        
    }
    UIImageView * lastImg =(UIImageView *)[_btnView viewWithTag:lastIndex+20];
    lastImg.image =[UIImage imageNamed:normalArr[lastIndex]];
    
    UIImageView * Img =(UIImageView *)[_btnView viewWithTag:index+20];
    Img.image =[UIImage imageNamed:slectArr[index]];
    
    UILabel * lab =(UILabel *)[_btnView viewWithTag:lastIndex+10];
    lab.textColor =[UIColor blackColor];
    
    UILabel * lab2 =(UILabel *)[_btnView viewWithTag:index+10];
    lab2.textColor =btnColor;
    
    lastIndex =index;
}

-(void)createImageView
{
    _topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,250)];
    
    
     UIImageView *  _imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image_brand_detail_topbg.jpg"]];
    _imageView.frame =CGRectMake(0,-200, self.view.frame.size.width, 400);
    
    [_topView addSubview:_imageView];
    

    UIImageView * titleImage =[[UIImageView alloc]init];
    titleImage.center =CGPointMake(self.view.center.x, _topView.center.y-80);
    titleImage.bounds =CGRectMake(0, 0, 70, 70);
    
    titleImage.layer.cornerRadius = 35;
    titleImage.clipsToBounds =YES;
    
    [titleImage sd_setImageWithURL:[NSURL URLWithString:_imageName]];
    [_topView addSubview:titleImage];
    
    UILabel * lab =[[UILabel alloc]init];
    lab.center =CGPointMake(_topView.center.x, titleImage.center.y+80);
    lab.bounds =CGRectMake(0, 0, 150, 30);
    lab.textAlignment =NSTextAlignmentCenter;
    lab.text =_labText;
    [_topView addSubview:lab];
    
    NSArray * btnArr =@[@"联系卖家",@"收藏"];
    //背景图片
    NSArray * imageArr =@[@"button_brand_detail_chat",@"button_brand_detail_like"];
    //按钮上的图片
    NSArray * btnImage=@[@"button_brand_detail_chat",@"button_product_numberadd_enable"];
    
    //返回分享按钮
    NSArray * backBtnArr =@[@"brand_button_xbacknew",@"brand_button_share"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:imageArr[0]] forState:UIControlStateNormal];
//    button.backgroundColor =[UIColor clearColor];
    
    [button addTarget:self action:@selector(say) forControlEvents:UIControlEventTouchUpInside];
    [_topView addSubview:button];
        
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(40);
        make.top.equalTo(lab.mas_bottom).offset(10);
        make.bottom.offset(-60);

            
    }];
        

    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

    button1.layer.cornerRadius = 5;
    button1.clipsToBounds =YES;
    button1.backgroundColor =[UIColor lightTextColor];
    [button1 addTarget:self action:@selector(stow) forControlEvents:UIControlEventTouchUpInside];
    [_topView addSubview:button1];
    
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(lab.mas_bottom).offset(10);
        make.bottom.offset(-60);
        make.left.equalTo(button.mas_right).with.offset(60);
        make.width.equalTo(button.mas_width);
        make.right.offset(-40);
    }];
    
    UIImageView * imageView =[[UIImageView alloc]init];
    imageView.image =[UIImage imageNamed:btnImage[0]];
    [button addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.offset(5);
        make.bottom.offset(-5);
        make.width.equalTo(@30);
    }];
    
    UIImageView * imageView2 =[[UIImageView alloc]init];
    imageView2.image =[UIImage imageNamed:btnImage[1]];
    [button1 addSubview:imageView2];
    
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(30);
        make.top.offset(5);
        make.bottom.offset(-5);
        make.width.equalTo(@30);
    }];
    
    
    UILabel * lab1 =[[UILabel alloc]init];
    lab1.text =btnArr[0];
    lab1.textAlignment =NSTextAlignmentCenter;
    lab1.font =[UIFont systemFontOfSize:15];
    [button addSubview:lab1];

    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(5);
        make.top.offset(5);
        make.bottom.offset(-5);
        make.right.offset(-15);
   
    }];
    
    UILabel * lab2 =[[UILabel alloc]init];
    lab2.text =btnArr[1];
    lab2.textAlignment =NSTextAlignmentCenter;
    lab2.font =[UIFont systemFontOfSize:15];
    [button1 addSubview:lab2];
    
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.equalTo(imageView2.mas_right).offset(5);
        make.top.offset(5);
        make.bottom.offset(-5);
        make.right.offset(-30);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    
    [backBtn setBackgroundImage:[UIImage imageNamed:backBtnArr[0]] forState:UIControlStateNormal];
//    backBtn.tag =i+20;
    [backBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    [self.view bringSubviewToFront:backBtn];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.offset(20);
        make.top.offset(40);
        make.width.equalTo(@50);
        make.height.equalTo(@50);
        
    }];
        
        
    UIButton *backBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn2 setBackgroundImage:[UIImage imageNamed:backBtnArr[1]] forState:UIControlStateNormal];
    [backBtn2 addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn2];
    [self.view bringSubviewToFront:backBtn2];

    [backBtn2 mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.right.offset(-20);
         make.top.offset(40);
         make.width.equalTo(backBtn.mas_width);
         make.height.equalTo(backBtn.mas_height);
         
     }];
    
}
-(void)say
{
    
        //联系卖家--
}
-(void)stow
{
    NSLog(@"1654654");
}
-(void)backBtn
{
  
        [self.navigationController popToRootViewControllerAnimated:YES];
    
    
 
}
-(void)share
{
    //分享
    
}
         
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.contentView.backgroundColor =[UIColor whiteColor];
    
    UIImageView * imageView1 =(UIImageView *)[cell.contentView viewWithTag:cell.tag+10];
    if (imageView1==nil)
    {
        imageView1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width/2-10, 240)];
        imageView1.tag =cell.tag+10;
        
        [cell.contentView addSubview:imageView1];
    }
    
    UILabel * lab1 =(UILabel *)[cell.contentView viewWithTag:cell.tag +200];
    if (lab1 ==nil)
    {
        lab1 =[[UILabel alloc]initWithFrame:CGRectMake(0, 240, self.view.bounds.size.width/2-10, 50)];
        lab1.tag =cell.tag +200;
        
        lab1.font =[UIFont systemFontOfSize:14];
        lab1.numberOfLines =0;
        
        [cell.contentView addSubview:lab1];
    }
    UILabel * lab2 =(UILabel *)[cell.contentView viewWithTag:cell.tag +500];
    if (lab2 ==nil)
    {
        lab2 =[[UILabel alloc]initWithFrame:CGRectMake(0, 280, self.view.bounds.size.width/2-10, 20)];
        lab2.tag =cell.tag +500;
        
        lab2.textColor =[UIColor magentaColor];
        [cell.contentView addSubview:lab2];
    }
    XSShop * shop =_dataArray[indexPath.row];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:shop.taobao_pic_url]];
    
    lab1.text =shop.taobao_title;
    lab2.text =[NSString stringWithFormat:@"%@ %@",shop.money_symbol,shop.taobao_selling_price];
    
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
